package com.wb.adutils.ads.pangle

import java.lang.Exception
import android.app.Activity
import android.os.Bundle
import android.view.ViewGroup
import com.bytedance.sdk.openadsdk.*
import com.wb.adutils.AdBaseLoader
import com.wb.adutils.db.AdEntity
import com.wb.adutils.tools.AdConstants

import com.wb.adutils.tools.LogC


class PangleRewardAdUtils(val adEntity: AdEntity?): AdBaseLoader(adEntity) {

    private var mttRewardAd: TTRewardVideoAd? = null

    override fun excLoadAd(activity: Activity) {
        try {
            if (isAdLoadSuccess) {
                LogC.e("pangle -> 激励 -> 使用缓存广告")
                listener?.onAdLoaded()
                return
            }
            LogC.e("pangle -> 激励 -> 开始请求激励广告")
            isAdLoadSuccess = false

            val mTTAdNative = TTAdSdk.getAdManager().createAdNative(activity)
            val adSlot = AdSlot.Builder()
                .setCodeId(adEntity?.adUnit) //模板广告需要设置期望个性化模板广告的大小,单位dp,激励视频场景，只要设置的值大于0即可
                .setSupportDeepLink(true)
                .setOrientation(orientation) //必填参数，期望视频的播放方向：TTAdConstant.HORIZONTAL 或 TTAdConstant.VERTICAL
                .setAdLoadType(TTAdLoadType.LOAD) //推荐使用，用于标注此次的广告请求用途为预加载（当做缓存）还是实时加载，方便后续为开发者优化相关策略
                .build()
            mTTAdNative.loadRewardVideoAd(adSlot, object : TTAdNative.RewardVideoAdListener {
                override fun onError(code: Int, message: String?) {
                    LogC.e("pangle -> 激励 -> onRewardVideoLoadFail：$message")
                    excAdLoadFail(code)
                }

                override fun onRewardVideoAdLoad(p0: TTRewardVideoAd?) {
                }

                override fun onRewardVideoCached() {}

                override fun onRewardVideoCached(rewardAd: TTRewardVideoAd?) {
                    if (timeoutDisposable?.isDisposed == true) { // 已超时
                        LogC.e("pangle -> 激励 -> onRewardVideoCached...已超时")
                        return
                    }
                    mttRewardAd = rewardAd
                    LogC.e("pangle -> 激励 -> onRewardVideoCached...")
                    timeoutDisposable?.dispose()
                    isAdLoadSuccess = true
                    listener?.onAdLoaded(null)
                }
            })
        } catch (e: Exception) {
            e.printStackTrace()
            timeoutDisposable?.dispose()
            listener?.onAdLoadFail(AdConstants.AD_ERROR_CODE_EXCEPTION)
            release()
        }
    }

    override fun showAd(adRoot: ViewGroup?) {
        LogC.e("pangle -> 激励 -> showAd...")
        val activity = getActivity()
        if (activity == null) {
            listener?.onAdLoadFail(AdConstants.AD_ERROR_CODE_ACTIVITY_IS_NULL)
            release()
            return
        }
        if (isAdLoadSuccess && mttRewardAd != null) {
            LogC.e("pangle -> 激励 -> showAd...显示")
            //展示广告，并传入广告展示的场景
            mttRewardAd?.setRewardAdInteractionListener(object : TTRewardVideoAd.RewardAdInteractionListener {
                var isRewardVerify = false
                //广告的展示回调
                override fun onAdShow() {
                    LogC.e("pangle -> 激励 -> showAd...onRewardedAdShow...")
                    listener?.onAdDisplay()
                }

                //广告的下载bar点击回调
                override fun onAdVideoBarClick() {
                    LogC.e("pangle -> 激励 -> showAd...onRewardClick...")
                    listener?.onAdClicked()
                }

                //广告关闭的回调
                override fun onAdClose() {
                    LogC.e("pangle -> 激励 -> showAd...onRewardedAdClosed...")
                    mttRewardAd = null
                    listener?.onAdComplete(isRewardVerify)
                    release()
                }

                //视频播放完毕的回调
                override fun onVideoComplete() {}
                override fun onVideoError() {
                    LogC.e("pangle -> 激励 -> showAd...onVideoError...")
                    listener?.onAdLoadFail(AdConstants.AD_ERROR_CODE_INTERNAL)
                    release()
                }

                override fun onRewardVerify(p0: Boolean, p1: Int, p2: String?, p3: Int, p4: String?) {
                    LogC.e("pangle -> 激励 -> showAd...onRewardVerify...")
                    isRewardVerify = true
                }

                override fun onRewardArrived(p0: Boolean, p1: Int, p2: Bundle?) {
                }

                //跳过视频播放
                override fun onSkippedVideo() {
                    LogC.e("pangle -> 激励 -> showAd...onSkippedVideo...")
                }
            })
            mttRewardAd?.showRewardVideoAd(activity)
        } else {
            timeoutDisposable?.dispose()
            listener?.onAdLoadFail(AdConstants.AD_ERROR_CODE_NOT_LOAD_SUCCESS)
            release()
        }
    }



    override fun release() {
        super.release()
        mttRewardAd = null
    }


}